<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
	
	private $baseurl;
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form','html','url'));
		$this->load->library('form_validation');
		$this->load->library('input');
		$this->load->model('admin_model','am');
		$this->baseurl = base_url();
		session_start();
	}
	
	//LOGIN DEL ZA ADMINOT
	public function index()
	{
		if(isset($_SESSION['admin']) && $_SESSION['admin'] == true)
		{
			header("Location: ".$this->baseurl."index.php/welcome/mlogiran");
			exit;
		}
		$this->form_validation->set_rules('user','username','required');
		$this->form_validation->set_rules('pass','password','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('login_view');
		}
		else
		{
			$data=array('user'=>$this->input->post('user'),
			'pass'=>$this->input->post('pass'));
			//$this->am->proveri_profil($data);
			if($this->am->proveri_profil($data))
			{
				$_SESSION['admin'] = true;
				//$_SESSION['user'] = $this->input->post('user');
				header("Location: ".$this->baseurl."index.php/welcome/mlogiran");
			}
			else 
			{
				header("Location: ".$this->baseurl."index.php/welcome/mNelogiran");
			}
		}
		
	}
	
	public function register()
	{
		$this->form_validation->set_rules('user','username','required');
		$this->form_validation->set_rules('pass','password','required');
		$this->form_validation->set_rules('ime','ime','required');
		$this->form_validation->set_rules('prezime','prezime','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('register_view');
		}
		else
		{
			$data=array('ime'=>$this->input->post('ime'),
						'prezime'=>$this->input->post('prezime'),
						'username'=>$this->input->post('user'),
						'password'=>$this->input->post('pass'),
						'status'=>'inactive');
			$this->am->registriraj_profil($data);
			header("Location: ".$this->baseurl."index.php/korisnik");
		}
	}
	
	public function admini()
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		if($this->input->post('submit') && $this->input->post('submit')=="Make Admin")
		{
			$username = $this->input->post('username');
			$this->am->azuriraj_admin($username);
		}
		$data['admini']=$this->am->prikaz_admini();
		$this->load->view('admini_view',$data);
	}
	
	//OBICNO VIEW DEKA IMA GRESKA VO USER. ILI PASS.
	public function mNelogiran()
	{
		$this->load->view('errorLogin_view');
	}
	
	//PRIKAZ KOGA ADMINOT KE SE LOGIRA
	public function mlogiran()
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$this->form_validation->set_rules('imeK','cateory name','required');
		$this->form_validation->set_rules('opisK','cateory description','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['kategorii']=$this->am->prikaz_kategorii();
			$this->load->view('logiran_view',$data);
		}
		else
		{
			$data=array('ime'=>$this->input->post('imeK'),'opis'=>$this->input->post('opisK'));
			if($this->am->vnesi_kategorija($data))
			{
				header("Location: ".$this->baseurl."index.php/welcome/mlogiran");
			}
			else
			{
				echo"ne e ok";
			}
		}
		
	}
	
	//BRISENJE KATEGORIJA
	public function mbrisiK($id)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$datoteki = $this->am->brisiK($id);
		if(count($datoteki)>0)
		{
			foreach ($datoteki as $datoteka)
			{
				$file = './uploads/'.$datoteka['ime'];
				unlink($file);
			}
		}
		header("Location: ".$this->baseurl."index.php/welcome/mlogiran");
	}
	
	//AZURIRANJE KATEGORIJA
	public function mupdateK($id)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$_SESSION['kategorija'] = $id;
		$this->form_validation->set_rules('imeK','cateory name','required');
		$this->form_validation->set_rules('opisK','cateory description','required');
		
		$data=$this->am->zemiKategorija($id);
		//print_r($data);
		$pole['kategorija']=$data['0'];
		if($this->form_validation->run()==FALSE)
		{
			$this->load->view('updateK_view',$pole);
		}
		else 
		{
			$dat=array('id'=>$id,'ime'=>$this->input->post('imeK'),'opis'=>$this->input->post('opisK'));
			$this->am->azurirajKategorija($dat);
			header("Location: ".$this->baseurl."index.php/welcome/mlogiran");
		}
	}
	//KOGA KLIKAME NA IME NA KATEGORIJA
	public function motvoriK($id)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$_SESSION['kategorija'] = $id;
		$this->form_validation->set_rules('imePK','subcateory name','required');
		$this->form_validation->set_rules('opisPK','subcateory description','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['podkategorii']=$this->am->prikazPodkategorii($id);
			$this->load->view('podkategorii_view',$data);
		}
		else
		{
			$data=array('ime'=>$this->input->post('imePK'),'opis'=>$this->input->post('opisPK'),
			'konKategorija'=>$id);
			if($this->am->vnesiPodkategorija($data))
			{
				header("Location: ".$this->baseurl."index.php/welcome/motvoriK/".$id);
			}
			else
			{
				echo"ne e ok";
			}
		}
	}
	
	//BRISENJE PODKATEGORIJA
	public function mbrisiPK($idk,$idpk)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		
		$datoteki = $this->am->brisiPK($idpk);
		if(count($datoteki)>0)
		{
			foreach ($datoteki as $datoteka)
			{
				$file = './uploads/'.$datoteka['ime'];
				unlink($file);
			}
		}
		header("Location: ".$this->baseurl."index.php/welcome/motvoriK/".$idk);
	}
	//AZURIRANJE PODKATEGORIJA
	public function mupdatePK($idk,$idpk)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$_SESSION['podkategorija'] = $idpk;
		$this->form_validation->set_rules('imePK','subcategory name','required');
		$this->form_validation->set_rules('opisPK','subcategory description','required');
		
		$data=$this->am->zemiPodkategorija($idpk);
		//print_r($data);
		$pole['podkategorija']=$data['0'];
		if($this->form_validation->run()==FALSE)
		{
			$this->load->view('updatePK_view',$pole);
		}
		else
		{
			$dat=array('id'=>$idpk,'ime'=>$this->input->post('imePK'),'opis'=>$this->input->post('opisPK'));
			$this->am->azurirajPodkategorija($dat);
			header("Location: ".$this->baseurl."index.php/welcome/motvoriK/".$idk);
		}
	}
	
	//KOGA KLIKAME NA IME NA PODKATEGORIJA
	public function motvoriPK($id)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$_SESSION['podkategorija'] = $id;
		$this->form_validation->set_rules('imeN','event name','required');
		$this->form_validation->set_rules('opisN','event description','required');
		$this->form_validation->set_rules('datumN','event date','required');
		$this->form_validation->set_rules('ucesniciN','event guest','required');
	
		if ($this->form_validation->run() == FALSE)
		{
			$data['nastani']=$this->am->prikazNastani($id);
			$this->load->view('nastani_view',$data);
		}
		else
		{
			$data=array('ime'=>$this->input->post('imeN'),'opis'=>$this->input->post('opisN'),
				'data'=>$this->input->post('datumN'),'ucesnici'=>$this->input->post('ucesniciN'),
			'konPKategorija'=>$id);
			if($this->am->vnesiNastan($data))
			{
				$p_id=$this->db->insert_id();
				header("Location: ".$this->baseurl."index.php/welcome/muploadF/".$id."/".$p_id);
			}
			else
			{
				echo"ne e ok";
			}
		}
	}
	//UPLOAD NA DATOTEKI
	public function muploadF($id,$pid)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$data = array();
		$_SESSION['nastan'] = $pid;
		$data1['id']=$id;
		//$this->load->view('upload_view',$data1);
		if($this->input->post('submit') && $this->input->post('submit')=='Upload')
		{
			$config['upload_path'] = './uploads/';
			$config['max_size']	= '3000';
			$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx|ppt|pptx|xls|xlsx';
			
			$this->load->library('upload', $config);
			
			if ( ! $this->upload->do_upload('file'))
			{
				$error = array('error' => $this->upload->display_errors());
				$data['error'] = $error;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				//print_r($data);
				$pole=array('ime'=>$data['upload_data']['file_name'],
				'tip'=>$data['upload_data']['file_ext'],
				'konNastan'=>$pid);
				$data['message'] = "The file was successfully uploaded!";
				$this->am->vnesiFile($pole);
				//if($this->am->vnesiFile($pole))
				//{
				//	header("Location: ".$this->baseurl."index.php/welcome/muploadF/".$id."/".$pid);
				//}
				//else {echo"error";}
			}
		}
		$this->load->view('upload_view', $data);
	}
	//BRISENJE NASTAN
	public function mbrisiN($idpk,$idn)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$datoteki = $this->am->brisiN($idn);
		if(count($datoteki)>0)
		{
			foreach ($datoteki as $datoteka)
			{
				$file = './uploads/'.$datoteka['ime'];
				unlink($file);
			}
		}
		header("Location: ".$this->baseurl."index.php/welcome/motvoriPK/".$idpk);
	}
	//AZURIRANJE NASTAN
	public function mupdateN($idpk,$idn)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$_SESSION['podkategorija'] = $idpk;
		$_SESSION['nastan'] = $idn;
		$this->form_validation->set_rules('imeN','event name','required');
		$this->form_validation->set_rules('opisN','event description','required');
		$this->form_validation->set_rules('datumN','event date','required');
		$this->form_validation->set_rules('ucesniciN','event guest','required');
	
		$data['0']=$this->am->zemiNastan($idn);
		$data['1']=$this->am->zemiDatoteki($idn);
		//print_r($data);
		$pole['nastani']=$data['0']['0'];
		$pole['datoteki']=$data['1'];
		
		$pole['idpk']=$idpk;
		
		if($this->form_validation->run()==FALSE)
		{
			$this->load->view('updateN_view',$pole);
		}
		else
		{
			$dat=array('id'=>$idn,
			'ime'=>$this->input->post('imeN'),
			'opis'=>$this->input->post('opisN'),
			'data'=>$this->input->post('datumN'),
			'ucesnici'=>$this->input->post('ucesniciN'));
			$this->am->azurirajNastan($dat);
			header("Location: ".$this->baseurl."index.php/welcome/motvoriPK/".$idpk);
		}
	}
	
	public function mbrisiD($idD,$idpk,$idn)
	{
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		$pole = $this->am->zemiD($idD);
		$pole = $pole[0];
		$myFile = './uploads/'.$pole['ime'];
		unlink($myFile);
		$this->am->brisiD($idD);
		header("Location: ".$this->baseurl."index.php/welcome/mupdateN/".$idpk."/".$idn);
	}
	//ZA UPLOAD NA DAT. PRI EDIT NA NASTAN
	public function muploadMF($idn)
	{
		$data = array();
		if(!isset($_SESSION['admin']) || $_SESSION['admin']!=true)
		{
			header("Location: ".$this->baseurl."index.php/korisnik");
			exit;
		}
		//$this->load->view('uploadMORE_view');
		if($this->input->post('submit') && $this->input->post('submit')=='Upload')
		{
			$config['upload_path'] = './uploads/';
			$config['max_size']	= '3000';
			$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx|ppt|pptx|xls|xlsx';
				
			$this->load->library('upload', $config);
				
			if ( ! $this->upload->do_upload('file'))
			{
				$error = array('error' => $this->upload->display_errors());
				$data['error'] = $error;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				//print_r($data);
				$pole=array('ime'=>$data['upload_data']['file_name'],
					'tip'=>$data['upload_data']['file_ext'],
					'konNastan'=>$idn);
				$data['message'] = "The file was successfully uploaded!";
				$this->am->vnesiFile($pole);
				//if($this->am->vnesiFile($pole))
				//{
				//	header("Location: ".$this->baseurl."index.php/welcome/muploadMF/".$idn);
				//}
				//else {echo"error";}
			}
		}
		$this->load->view('uploadMORE_view', $data);
	}
	
	public function logout()
	{
		session_destroy();
		header("Location: ".$this->baseurl."index.php/welcome/index");
		exit;
	}
}
?>